﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Controls;
using SimpleTool.Base;
using System.Security.AccessControl;
using static SimpleTool.SerialTool.SerialControl.SerialViewModel;
using System.Windows.Input;
using Supabase.Postgrest.Models;
using System.ComponentModel.DataAnnotations.Schema;
using Supabase.Postgrest.Attributes;

namespace SimpleTool.SerialTool.SerialControl
{
    public class PublicSectionModel
    {
        private SelectionMode ListBoxModeTemplate = SelectionMode.Single;

        public SelectionMode ListBoxMode
        {
            set
            {
                ListBoxModeTemplate = value;
            }
            get
            {
                return ListBoxModeTemplate;
            }
        }

        private string ListBoxModeNameTemplate = "\ue64b";

        public string ListBoxModeName
        {
            set
            {
                ListBoxModeNameTemplate = value;
            }
            get
            {
                return ListBoxModeNameTemplate;
            }
        }
    }

    public class CommandSectionModel
    {
        public delegate void CommandChangeReceive(object? sender, CommandChangeEventHandle e);

        public ICommand? CommandAddedCommand;

        public ICommand? CommandTakeCommand;

        public ICommand? CommandIndexCommand;

        public ICommand? CommandWriteCommand;

        public ICommand? CommandModeCommand;

        public ICommand? CommandEditCommand;

        /// <summary>
        /// defualt duration 0; default readonly true
        /// </summary>
        public sealed class CommandStruct
        {
            public string Command { set; get; }
            public string Alias { set; get; }
            public int Duration { set; get; }
            public int Order { set; get; }

            public CommandStruct(
                string _command = "", 
                int _duration = 0, 
                string alias = "",
                int order = 0)
            {
                Command = _command; Duration = _duration; Alias = alias; Order = order;
            }
        }

        public sealed class AlterCommandStruct
        {
            public CommandStruct AlterCommand { set; get; }

            public int AlterIndex { set; get; }

            public AlterCommandStruct(CommandStruct newStruct, int newIndex)
            {
                AlterCommand = newStruct; AlterIndex = newIndex;

            }
        }

        public CommandStruct? CurrentItem { set; get; } //缓存当前选中的指令项

        public List<CommandStruct> CommandSelectionsCache = []; //缓存选中的指令项
        public ObservableCollection<CommandStruct> CommandListItems { set; get; } = []; //指令项集合
    }

    public class ConfigSectionModel
    {
        public ICommand? Srilconnect;

        public ICommand? AtrributeChangeCommand;

        public sealed class ConfigSelectedRecord
        {
            private ConfigSelectedRecord() { }

            public static readonly ConfigSelectedRecord Com = new();

            public string PortName { get; set; } = string.Empty;

            public int BaudRate { get; set; } = 115200;

            public StopBits StopBit { get; set; } = System.IO.Ports.StopBits.One;

            public Parity ParityBit { get; set; } = System.IO.Ports.Parity.None;

            public int DataBit { get; set; } = 8;

            public Handshake Handshake { get; set; } = System.IO.Ports.Handshake.None;

        }

        public sealed class SerialConfigInfoMap
        {

            private SerialConfigInfoMap() { }

            public static readonly SerialConfigInfoMap Com = new();

            public Dictionary<string, string> SerialComNameMap { get; set; } = [];

            public Dictionary<string, int> SerialComBaudMap { get; set; } = [];

            public Dictionary<string, int> SerialComDataMap { get; set; } = [];

            public Dictionary<string, Parity> SerialComParityMap { get; set; } = [];

            public Dictionary<string, StopBits> SerialComStopMap { get; set; } = [];

            public Dictionary<string, Handshake> SerialComHandShakeMap { get; set; } = [];
        }

        [Supabase.Postgrest.Attributes.Table("serial_config")]
        public sealed class SerialConfig : BaseModel
        {
            [Supabase.Postgrest.Attributes.Column("port_name")]
            [Supabase.Postgrest.Attributes.PrimaryKey]
            public string Portname { set; get; } = "COM27";

            [Supabase.Postgrest.Attributes.Column("baud_rate")]
            public string Baudrate { set; get; } = string.Empty;

            [Supabase.Postgrest.Attributes.Column("data_bits")]
            public string Databits { set; get; } = string.Empty;

            [Supabase.Postgrest.Attributes.Column("stop_bits")]
            public string Stopbits { set; get; } = string.Empty;

            [Supabase.Postgrest.Attributes.Column("parity_bits")]
            public string Paritybits { set; get; } = string.Empty;

            [Supabase.Postgrest.Attributes.Column("hand_shake")]
            public string Handshake { set; get; } = string.Empty;

        }

        [Supabase.Postgrest.Attributes.Table("serial_command")]
        public sealed class SerialCommand : BaseModel
        {
            [Supabase.Postgrest.Attributes.Column("command_name"), Supabase.Postgrest.Attributes.PrimaryKey]
            public string Commandname { set; get; } = string.Empty;

            [Supabase.Postgrest.Attributes.Column("command_alias")]
            public string Commandalias { set; get; } = string.Empty;

            [Supabase.Postgrest.Attributes.Column("command_interval")]
            public string Commandinterval { set; get; } = string.Empty; 
        }

        public bool PublicConnectEnable { set; get; } = false;

        public Dictionary<string, string> TableReserveDuty = new()
        {
            {"serial_config", "CREATE TABLE IF NOT EXISTS serial_config(" +
                "port_name TEXT PRIMARY KEY," +
                "baud_rate TEXT NOT NULL COLLATE NOCASE," +
                "data_bits TEXT NOT NULL COLLATE NOCASE," +
                "stop_bits TEXT NOT NULL COLLATE NOCASE," +
                "parity_bits TEXT NOT NULL COLLATE NOCASE," +
                "hand_shake TEXT NOT NULL COLLATE NOCASE)"}
            ,

            {"serial_command", "CREATE TABLE IF NOT EXISTS serial_command(" +
                "command_name TEXT PRIMARY KEY," +
                "command_alias TEXT NOT NULL COLLATE NOCASE," +
                "command_interval TEXT NOT NULL COLLATE NOCASE)"}

        };
        public string SelectedSerialProt { set; get; } = string.Empty;
        public string SelectedBaudRate { set; get; } = string.Empty; 
        public string SelectedDataBit { set; get; } = string.Empty;
        public string SelectedParityBit { set; get; } = string.Empty;
        public string SelectedStopBit { set; get; } = string.Empty;
        public string SelectedHandShake { set; get; } = string.Empty;
        public ObservableCollection<string> SerialProts { set; get; } = [];
        public ObservableCollection<string> BaudRates { set; get; } = [];
        public ObservableCollection<string> DataBits { set; get; } = [];
        public ObservableCollection<string> ParityBits { set; get; } = [];
        public ObservableCollection<string> StopBits { set; get; } = [];
        public ObservableCollection<string> HandShakes { set; get; } = [];
        public string ExecuteButtonText { set; get; } = "连接";
        public bool SubmitButtonCanExecute { set; get; } = true;

    }
}
